package com.itheima.tliasmanagent.controller;

import com.itheima.tliasmanagent.config.AliyunOSSProperties;
import com.itheima.tliasmanagent.pojo.Result;
import com.itheima.tliasmanagent.utils.AliyunOSSUtils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@Slf4j
public class UploadController {

    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;




    /*@PostMapping("/upload")
    public Result upload( MultipartFile file) throws IOException {
        // 获取原始文件名称
        String originalFilename = file.getOriginalFilename();
        // 构建新名称
        String newFileName= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        // 将上传文件保存到本地文件
        file.transferTo(new File("D:/"+newFileName));
        return Result.success();
    }*/
    @PostMapping("/upload")
    public Result upload( MultipartFile file) throws Exception {
        // 上传到OSS
        String extName=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
       String url= AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(),aliyunOSSProperties.getBucketName(),file.getBytes(),extName);
        return Result.success(url);
    }

}
